package cn.genmer.test.security.annotation;

import java.util.HashMap;
import java.util.Map;

public class TimedContext {

    private static final ThreadLocal<Map<String, Long>> timeRecords = ThreadLocal.withInitial(HashMap::new);

    public static void start(String methodName) {
        timeRecords.get().put(methodName, System.currentTimeMillis());
    }

    public static long stop(String methodName) {
        long endTime = System.currentTimeMillis();
        Long startTime = timeRecords.get().get(methodName);
        long duration = endTime - startTime;
        timeRecords.get().put(methodName, duration);
        return duration;
    }

    public static Map<String, Long> getTimeRecords() {
        return timeRecords.get();
    }

    public static void clear() {
        timeRecords.remove();
    }
}
